/*
 * @Author: https://github.com/WangEn
 * @Author: https://gitee.com/lovetime/
 * @Date:   2018-03-27
 * @lastModify 2018-3-28
 * +----------------------------------------------------------------------
 * | WeAdmin 表格table中多个删除等操作公用js
 * | 有改用时直接复制到对应页面也不影响使用
 * +----------------------------------------------------------------------
 */
layui.extend({
  admin: 'http://localhost:8080/js/admin'
});
layui.use(['laydate', 'jquery', 'admin'], function () {
  var laydate = layui.laydate,
    $ = layui.jquery,
    admin = layui.admin;
  //执行一个laydate实例
  laydate.render({
    elem: '#start' //指定元素
  });
  //执行一个laydate实例
  laydate.render({
    elem: '#end' //指定元素
  });
  /*用户-停用*/
  window.member_stop = function (obj, username) {

      // 发送ajax请求，修改管理员启用状态
      // $.ajax({
      //     url: 'http://localhost:8080/admin/user/changeState',
      //     type: 'POST',
      //     dataType: 'json',
      //     data: {
      //         "username":username
      //     },
      //     success: function (res) {
      //         console.log("success - " + res);
      //         if (res.result === "SUCCESS") {
      //
      //         } else {
      //             layer.msg(res.message);
      //         }
      //     }
      // });
    if ($(obj).attr('title') === '启用') {
        layer.confirm('确认要启用吗', function () {
            $.ajax({
                url: 'http://localhost:8080/admin/user/changeState',
                type: 'POST',
                dataType: 'json',
                data: {
                    "username":username
                },
                success: function (res) {
                    console.log("success - " + res);
                    if (res.result === "SUCCESS") {
                        $(obj).attr('title', '停用')
                        $(obj).find('i').removeClass('layui-icon-upload-circle').addClass('layui-icon-download-circle');
                        layer.msg('已启用!', {
                            icon: 4,
                            time: 1000,
                            end: function () {
                                location.reload();
                            }
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
        });
    } else {
        layer.confirm('确认要停用吗', function () {
            $.ajax({
                url: 'http://localhost:8080/admin/user/changeState',
                type: 'POST',
                dataType: 'json',
                data: {
                    "username":username
                },
                success: function (res) {
                    console.log("success - " + res);
                    if (res.result === "SUCCESS") {
                        $(obj).attr('title', '启用')
                        $(obj).find('i').removeClass('layui-icon-download-circle').addClass('layui-icon-upload-circle');
                        layer.msg('已停用!', {
                            icon: 5,
                            time: 1000,
                            end: function () {
                                location.reload();
                            }
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
        });
    }


  }

  /*用户-删除*/
  window.member_del = function (obj, username) {
    layer.confirm('确认要删除吗？', function (index) {
      //发异步删除数据
      // $(obj).parents("tr").remove();
        $.ajax({
            url: 'http://localhost:8080/admin/user/delete',
            type: 'POST',
            dataType: 'json',
            data: {
                "username":username
            },
            success: function (res) {
                if (res.result === "SUCCESS") {
                    console.log('删除成功');
                    layer.msg('已删除!', {
                        icon: 1,
                        time: 1000,
                        end: function () {
                            location.reload();
                        }
                    });
                } else {
                    layer.msg(res.message);
                }
            }
        });
    });
  }

  window.delAll = function (argument) {
    var data = admin.tableCheck.getData();
    console.log("data : " + data);
    if (data.length > 0) {
      layer.confirm('确认要删除吗？', function (index) {
        //捉到所有被选中的，发异步进行删除
          $.ajax({
              url: 'http://localhost:8080/admin/user/batch/delete',
              type: 'POST',
              dataType: 'json',
              contentType: 'application/json',
              data: JSON.stringify(data),
              success: function (res) {
                  if (res.result === "SUCCESS") {
                      console.log('删除成功');
                      layer.msg('删除成功!', {
                          icon: 1,
                          time: 500,
                          end: function () {
                              location.reload();
                          }
                      });
                  } else {
                      layer.msg(res.message);
                  }
              }
          });
      });
    } else {
      layer.msg("请先选择");
    }
  }
});